get_publish_date_for_rss_feed() {
    sed -n -e 's/<meta data-date=\"//p' "$1" |\
        sed -n -e 's/&lt;//p' |\
        sed -n -e 's/&gt;//p' |\
        sed -n -e 's/\" \/>//p' |\
        sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
}


get_html_element_title() {
    sed -n -e 's/<title>//p' "$1" |\
        sed -n -e 's/<\/title>//p' |\
        sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
}


get_html_element_meta_description() {
    sed -n -e 's/<meta name=\"description\" content=\"//p' "$1" |\
        sed -n -e 's/\" \/>//p' |\
        sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
}


get_link_for_rss_feed() {
    local current=${1:?}
    local input_directory=${2:?}
    local absolute_path=${3:?}
    echo "${current/$input_directory/$absolute_path}"
}


build_rss_main() {
    local input_directory=${1:?}
    local absolute_path=${2:?}
    local output_file=${3:?}
    local site_name=${4:?}
    local max_entries=${5:-10}


    if [[ ! $max_entries =~ ^-?[0-9]+$ ]] || [ "$max_entries" -lt 1 ]; then
        echo "Must have at least one entry, defaulting to 1."
        max_entries=1
    fi
    # if [ ! -d feeds ]; then
    #     mkdir feeds
    # fi
    if [ -f "$output_file" ]; then
        rm "${output_file}"
    else
        echo "Create new rss feed."
    fi
    mkdir -p "$(dirname "${output_file}")"


    {
        echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
        echo "<rss version=\"2.0\">"
        echo "  <channel>"
        echo "    <description/>"
        echo "    <lastBuildDate>$(date)</lastBuildDate>"
        echo "    <link>/</link>"
        echo "    <title>${site_name}</title>"
    } > "${output_file}"


    local path_array=()
    while IFS=  read -r -d $'\0'; do
        path_array+=("$REPLY")
    done < <(find "${input_directory}" -type f -name "*.html" -print0)
    unset IFS


    local wfmd_array=()
    for i in "${path_array[@]}"
    do
        item="$(get_publish_date_for_rss_feed "$i")|\
$(get_html_element_title "$i")|\
$(get_html_element_meta_description "$i")|\
$(get_link_for_rss_feed "$i" "${input_directory}" "${absolute_path}")"
        wfmd_array+=("${item}")
    done


    local sort_array=()
    # TODO: Should probably go full bash or posix. So, could do something like this:
    # IFS=$'\n' read -d '' -r -a sort_array < <(printf '%s\n' "${wfmd_array[@]}" | sort --reverse)
    readarray -t sort_array < <(printf '%s\n' "${wfmd_array[@]}" | sort --reverse)


    for j in "${sort_array[@]}"
    do
        if [ "$max_entries" -lt 1 ]; then
            break
        fi


        IFS='|' read -ra faux <<< "$j"; unset IFS
        if [ ! -z "${faux[0]}" ]; then
            max_entries=$((max_entries-1))
            {
                echo "    <item>"
                echo "      <pubDate>${faux[0]}</pubDate>"
                echo "      <description>${faux[2]}</description>"
                echo "      <link>${faux[3]}</link>"
                echo "      <title>${faux[1]}</title>"
                echo "    </item>"
            } >> "${output_file}"
        fi
    done


    {
        echo "  </channel>"
        echo "</rss>"
    } >> "${output_file}"
}
